package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/role")
public class RoleController {

    @Reference
    private RoleService roleService;

    @RequestMapping("/findById")
    public Result findById(Integer id){
        try {
            Role role = roleService.findById(id);
            return new Result(true, MessageConstant.GET_PERMISSION_SUCCESS,role);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_PERMISSION_FALL);
        }
    }

    @RequestMapping("/add")
    @PreAuthorize("hasAuthority('ROLE_ADD')")
    public Result add(@RequestBody Role role, Integer[] permissionIds){
        try {
            roleService.add(role,permissionIds);
            return new Result(true, MessageConstant.ADD_PERMISSION_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_PERMISSION_FALL);
        }
    }

    @RequestMapping("/edit")
    @PreAuthorize("hasAuthority('ROLE_EDIT')")
    public Result edit(@RequestBody Role role, Integer[] permissionIds){
        try {
            roleService.edit(role,permissionIds);
            return new Result(true, MessageConstant.EDIT_PERMISSION_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_PERMISSION_FALL);
        }
    }

    @RequestMapping("/delete")
    @PreAuthorize("hasAuthority('ROLE_DELETE')")
    public Result delete(Integer id){
        try {
            roleService.delete(id);
            return new Result(true, MessageConstant.DELETE_PERMISSION_SUCCESS);
        } catch (Exception e) {
            String message = e.getMessage();
            e.printStackTrace();
            return new Result(false,message);
        }
    }


    @RequestMapping("/findAllRole")
    public Result findAllRole(){
        try {
            List<Role> roleList =  roleService.findAllRole();
            return new Result(true,"查询所有角色成功",roleList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询失败");
        }
    }

    @RequestMapping("/findPage")
    @PreAuthorize("hasAuthority('ROLE_QUERY')")
    public PageResult findPage(@RequestBody QueryPageBean pageBean){
        return roleService.findPage(pageBean);
    }

    @RequestMapping("/findAll")
    public Result findAll(){
        try {
            List<Permission> list = roleService.findAll();
            return new Result(true, MessageConstant.GET_PERMISSION_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_PERMISSION_FALL);
        }
    }

    @RequestMapping("/findPermissionIdsByRole")
    public Result findPermissionIdsByRole(Integer permissionIds){
        try {
            List<Integer> list = roleService.findPermissionIdsByRole(permissionIds);
            return new Result(true,MessageConstant.GET_PERMISSION_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_PERMISSION_FALL);
        }
    }

    @RequestMapping("/findRolesByMenuId")
    public Result findRolesByMenuId(Integer menuId){
        try {
            List<Integer> list = roleService.findRolesByMenuId(menuId);
            return new Result(true,"查询成功",list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询失败");
        }
    }


}
